<?php
/**
 * Created by PhpStorm.
 * User: yxb
 * Date: 2016/12/19
 * Time: 9:16
 */
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Cache;
class Login extends Controller{
    /*
     * 登录
     * @param phone
     * @param password
     * */
    public function login($phone = '', $password = ''){
        if(empty(Db::name("members")->where('phone',$phone)->find())){
            return format_return(0,'',"此账号未注册");
        }
        $password = md5($password);
        $data = [
            'phone' => $phone,
            'password' => $password
        ];
        $member = Db::name("members")->where($data)->find();
        if(!empty($member)){
            $this->update_login($phone);
            unset($member);
            $member = Db::name("members")->where($data)->find();
            $data['uid'] = $member['uid'];
            $data['assess_token'] = $member['assess_token'];
            return format_return(1,$data);
        }else{
            return format_return(0,'',"密码错误");
        }
    }
    /*
     * 更新登录信息
     * */
    private function update_login($phone = ''){
        $last_logintime = time();
        $assess_token = join_assesstoken($phone,$last_logintime);
        $data = [
            'last_logintime' => $last_logintime,
            'assess_token' => $assess_token
        ];
        Db::name("members")->where('phone',$phone)->update($data);
    }
    /**
     * 注册
     * @param phone 手机
     * @param code 验证码
     * @param password 密码
     * */
    public function register($phone = '',$code = '',$password = ''){
        Db::name("log")->insertGetId( ['name'=>Cache::get('code_' . $phone)]);
        Db::name("log")->insertGetId(['name'=>$code]);
        $cache = Cache::get('code_' . $phone);
       // check_code($phone,$code) || $this->error("验证码错误");
        if($cache != $code){
            return format_return(0,'','验证码错误');
        }else{
            if(!empty(Db::name("members")->where('phone',$phone)->find())){
                return format_return(0,'',"此用户已注册");
            }
            $last_logintime = time();
            $data = [
                'phone' => $phone,
                'password' => md5($password),
                'last_logintime' => $last_logintime,
                'reg_time' => $last_logintime,
                'assess_token' => join_assesstoken($phone,$last_logintime)
            ];
            $uid = Db::name("members")->insertGetId($data);
            unset($data);
            $data['uid'] = $uid;
            $data['assess_token'] = join_assesstoken($phone,$last_logintime);
            return format_return(1,$data,"注册成功");
        }

    }

    /**
     * 忘记密码
     * @param phone
     * @param code
     * @param password
     * */
    public function update_password($phone = '',$code = '',$password = ''){
        if(empty(Db::name("members")->where('phone',$phone)->find())){
            return format_return(0,'',"此账号未注册，请先注册");
        }
        if(!check_phone($phone)){
            return format_return(0,'','手机号码格式错误');
        }
        if(!check_code($code,$phone)){
            return format_return(0,'','验证码错误');
        }
      //  check_phone() || $this->error("手机号码格式错误");
       // check_code($code,$phone) || $this->error("验证码错误");
        $res = Db::name("members")->where('phone',$phone)->update(['password'=>md5($password)]);
        if(!empty($res)){
            $member = Db::name("members")->where('phone',$phone)->find();
            $data['uid'] = $member['uid'];
            $data['assess_token'] = $member['assess_token'];
            return format_return(1,$data);
        }else{
           // $data['error'] = "修改失败";
            return format_return(0,'',"修改失败");
        }
    }

    /**
     * 发送验证码
     * @param string $phone
     */
    public function get_code($phone = '') {
        empty($phone) && $this->error('非法参数');
        check_phone($phone) || $this->error('手机号格式错误');
        if(Cache::get('code_' . $phone)){
            return format_return(0,'','请求太频繁，请稍后再试');
        }
        $code = rand(100000, 999999);
        Cache::set('code_' . $phone, $code, 60);
        send_sms($phone, $code);
        return format_return(1,'',"发送成功");
    }

}